﻿
jQuery.prototype.loadWithLoading = function (newElement, url, htmlNamePrefix, data, successCallback, errorCallback, htmlBefore, htmlAfter, insertionMode, cssLoadingClass, targetForm) {
    var target = this;
    var isTargetTemporary = false;

    if (htmlNamePrefix)
        url = url + (url.indexOf('?') > 0 ? '&' : '?') + 'HtmlNamePrefix=' + escape(htmlNamePrefix);

    var newElementIsTemporary = newElement == null || newElement == '';
    if (newElementIsTemporary)
        newElement = $('<div></div>');
    newElement = $(newElement).clone();
    /*If there is no target element, load into a temporary loading doc*/
    if (target.length == 1 && target[0] == document) {
        isTargetTemporary = true;
        target = $('<div style="display:none"/>').append(newElement).appendTo(target);
    } else {
        isTargetTemporary = false;
        newElement.addClass(cssLoadingClass);
        if (insertionMode == 'Append') {
            target.append(newElement);
        } else if (insertionMode == 'Replace') {
            target.empty();
            target.append(newElement);
        } else if (insertionMode == 'Prepend') {
            target.prepend(newElement);
        } else {
            alert('Invalid insertion mode: ' + insertionMode);
        }
    }

    var args = {
        data: data,
        url: url,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (targetForm)
                targetForm.find('[notSubmittingVal]').each(function () {
                    $(this).val($(this).attr('notSubmittingVal'));
                });

            newElement.remove();
            if (errorCallback)
                errorCallback.call(newElement, XMLHttpRequest, textStatus, errorThrown);
            else
                dialogAlert('Operation failed', 'Sent: ' + data + '<br/>To: ' + url + '<div style="text-align:left">' + XMLHttpRequest.responseText + '</div>', null, 900);
            if (isTargetTemporary)
                target.remove();
        },
        success:
            function (responseText, textStatus, XMLHttpRequest) {
                newElement.removeClass(cssLoadingClass);
                newElement.html(responseText);
                if (htmlBefore && htmlBefore.length > 0)
                    newElement.prepend(htmlBefore);
                if (htmlAfter && htmlAfter.length > 0)
                    newElement.append(htmlAfter);
                newElement.runElementDecorators();
                if (newElementIsTemporary) {
                    var responseElements = newElement.children();
                    newElement.after(responseElements);
                    newElement.remove();
                    newElement = responseElements;
                }
                if (successCallback)
                    successCallback.call(newElement, responseText, textStatus, XMLHttpRequest);
                if (isTargetTemporary)
                    target.remove();
            }
    };

    if (targetForm)
        $(targetForm).ajaxSubmit(args)
    else
        $.ajax(args);
};

$.elementDecorators = new Array();

$.addElementDecorator = function (decorator) {
    $.elementDecorators[$.elementDecorators.length] = decorator;
};

jQuery.prototype.runElementDecorators = function () {
    for (var i = 0; i < $.elementDecorators.length; ++i) {
        for (var j = 0; j < this.length; ++j) {
            $.elementDecorators[i].call(this[j], this[j]);
        }
    }
    return this;
};
